<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #box{
      width: 100px;
      height: 100px;
      background-color: #000;
    }
  </style>
</head>
<body>
  <div id="box"></div>
  <script>
    //获取元素
    var box = document.getElementById("box");
    //调用函数
    addEvent(box,"click",function(){
      alert(1);
    })

    //参数（事件源，事件类型，事件函数）
    function addEvent(ele, type, fn){
      if(ele.addEventListener){
        ele.addEventListener(type, fn);
      }else if (ele.attachEvent){
        ele.attachEvent("on"+type, fn);
      }else{
        ele.onclick = fn;
      }
    }
  </script>
</body>
</html>